/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swing;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

/**
 *
 * @author Iskandar Sarkawi
 */
public class Example3v2_a_GUI_Handler extends JFrame{

    private JTextField txtField1;
    private JTextField txtField2;
    private JTextField txtField3;
    private JPasswordField passwordField;

    public Example3v2_a_GUI_Handler(){
        super("Welcome to the Survey");
        setLayout(new FlowLayout());

        txtField1 = new JTextField(10);//set parameter to 10        
        add(txtField1);//add to window

        txtField2 = new JTextField("Enter text here");//set default text        
        add(txtField2);

        txtField3 = new JTextField("UnEditable", 20 );//set default text and the length        
        txtField3.setEditable(false);
        add(txtField3);

        passwordField = new JPasswordField("mypassword");        
        add(passwordField);

        //we need to add some brain to the four above item

        Handler handler = new Handler();
        txtField1.addActionListener(handler);
        txtField2.addActionListener(handler);
        txtField3.addActionListener(handler);
        passwordField.addActionListener(handler);

    }

    public class Handler implements ActionListener {

        public void actionPerformed(ActionEvent event){

            String message = "";

            if(event.getSource() == txtField1)
                message = String.format("Field 1: %s", event.getActionCommand());
            else if(event.getSource() == txtField2)
                message = String.format("Field 2: %s", event.getActionCommand());
            else if(event.getSource() == txtField3)
                message = String.format("Field 3: %s", event.getActionCommand());
            else if(event.getSource() == passwordField)
                message = String.format("Password field is: %s", event.getActionCommand());

            JOptionPane.showMessageDialog(null, message);
        }
    }

}
